//202102104015柴亚楠
package cn.edu.sdjzu.xg.kcsj.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "CharsetFilter",urlPatterns = {"/*"})
public class CharsetFilter implements Filter {
    public void init(FilterConfig config) throws ServletException{}
    public void doFilter(ServletRequest req, ServletResponse resp,
                         FilterChain chain) throws ServletException, IOException{
        System.out.println("CharsetFilter - encoding begins");
        HttpServletRequest request =(HttpServletRequest) req;
        HttpServletResponse response =(HttpServletResponse) resp;
        response.setContentType("application/json;charset=UTF-8");
        String method =request.getMethod();
        if(!"DELETE".contains(method)){
            request.setCharacterEncoding("UTF-8");
        }
        chain.doFilter(req,resp);
        System.out.println("CharsetFilter - encoding ends");
    }
    public void destroy() {
    }
}
